﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Threading.Tasks;

namespace Woodlands
{
    /// <summary>
    /// Interaction logic for StudentWindow.xaml
    /// </summary>
    public partial class StaffWindow : Page
    {
        Database database = new Database();

        public StaffWindow()
        {
            InitializeComponent();
            DataTable courseTable = new DataTable();
            Database.fillCourse().Fill(courseTable);
            staffIDTxt.Text = Staff.nextID();
        }    

        private void dobTxt_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (createStaffDOBTxt.Text == "YYYY/MM/DD")
            {
                createStaffDOBTxt.Text = "";
            }
        }

        private void dobTxt_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (createStaffDOBTxt.Text == "")
            {
                createStaffDOBTxt.Text = "YYYY/MM/DD";
            }
        }

        private async void displayAmend()
        {
            if (AmendStaffIDTxt.Text.Length >= 8)
            {
                Staff staff = await Staff.search(AmendStaffIDTxt.Text);
                if (staff.FirstName != "")
                {
                    amendStaffFirstNameTxt.Text = staff.FirstName;
                    amendStaffMiddleNameTxt.Text = staff.MiddleName;
                    amendStaffSurnameTxt.Text = staff.LastName;
                    amendStaffHomeNoTxt.Text = staff.HomePhone;
                    amendStaffMobNo.Text = staff.MobilePhone;
                    amendStaffPersonalEmailTxt.Text = staff.EmailPersonal;
                    amendStaffLine1Txt.Text = staff.Line1;
                    amendStaffLine2Txt.Text = staff.Line2;
                    amendStaffCityTxt.Text = staff.City;
                    amendStaffPostcodeTxt.Text = staff.Postcode;
                    amendStaffStatusCombo.SelectedIndex = 0;
                    amendStaffFirstNameTxt.IsEnabled = true;
                    amendStaffMiddleNameTxt.IsEnabled = true;
                    amendStaffSurnameTxt.IsEnabled = true;
                    amendStaffHomeNoTxt.IsEnabled = true;
                    amendStaffMobNo.IsEnabled = true;
                    amendStaffPersonalEmailTxt.IsEnabled = true;
                    amendStaffLine1Txt.IsEnabled = true;
                    amendStaffLine2Txt.IsEnabled = true;
                    amendStaffCityTxt.IsEnabled = true;
                    amendStaffPostcodeTxt.IsEnabled = true;
                    amendStaffStatusCombo.IsEnabled = true;
                }
                else
                {
                    amendStaffFirstNameTxt.IsEnabled = false;
                    amendStaffMiddleNameTxt.IsEnabled = false;
                    amendStaffSurnameTxt.IsEnabled = false;
                    amendStaffHomeNoTxt.IsEnabled = false;
                    amendStaffMobNo.IsEnabled = false;
                    amendStaffPersonalEmailTxt.IsEnabled = false;
                    amendStaffLine1Txt.IsEnabled = false;
                    amendStaffLine2Txt.IsEnabled = false;
                    amendStaffCityTxt.IsEnabled = false;
                    amendStaffPostcodeTxt.IsEnabled = false;
                    amendStaffStatusCombo.IsEnabled = false;
                }
            }
            else
            {
                amendStaffFirstNameTxt.Text = "";
                amendStaffMiddleNameTxt.Text = "";
                amendStaffSurnameTxt.Text = "";
                amendStaffHomeNoTxt.Text = "";
                amendStaffMobNo.Text = "";
                amendStaffPersonalEmailTxt.Text = "";
                amendStaffLine1Txt.Text = "";
                amendStaffLine2Txt.Text = "";
                amendStaffCityTxt.Text = "";
                amendStaffPostcodeTxt.Text = "";
                amendStaffStatusCombo.SelectedIndex = 0;
                amendStaffFirstNameTxt.IsEnabled = false;
                amendStaffMiddleNameTxt.IsEnabled = false;
                amendStaffSurnameTxt.IsEnabled = false;
                amendStaffHomeNoTxt.IsEnabled = false;
                amendStaffMobNo.IsEnabled = false;
                amendStaffPersonalEmailTxt.IsEnabled = false;
                amendStaffLine1Txt.IsEnabled = false;
                amendStaffLine2Txt.IsEnabled = false;
                amendStaffCityTxt.IsEnabled = false;
                amendStaffPostcodeTxt.IsEnabled = false;
                amendStaffStatusCombo.IsEnabled = false;
            }
        }

        private void staffIDTxtAmend_KeyUp(object sender, KeyEventArgs e)
        {
            displayAmend();
        }

        private async void displayArchive()
        {
            if (archiveStaffIDTxt.Text.Length >= 8)
            {
                Staff staff = await Staff.search(archiveStaffIDTxt.Text);
                if (staff.FirstName.Length == 0)
                {
                    archiveStaffFirstNameTxt.Text = "";
                    archiveStaffMiddleNameTxt.Text = "";
                    archiveStaffSurnameTxt.Text = "";
                    archiveStaffDOBTxt.Text = "";
                    archiveStaffCurrentStatusTxt.Text = "Invalid StaffID";
                }
                else
                {
                    archiveStaffFirstNameTxt.Text = staff.FirstName;
                    archiveStaffMiddleNameTxt.Text = staff.MiddleName;
                    archiveStaffSurnameTxt.Text = staff.LastName;
                    archiveStaffDOBTxt.Text = staff.DOB;
                    archiveStaffCurrentStatusTxt.Text = staff.Status;
                }
            }
            else if (archiveStaffIDTxt.Text.Length < 8)
            {
                archiveStaffFirstNameTxt.Text = "";
                archiveStaffMiddleNameTxt.Text = "";
                archiveStaffSurnameTxt.Text = "";
                archiveStaffDOBTxt.Text = "";
                archiveStaffCurrentStatusTxt.Text = "A staff ID must be 8 digits long";
            }
        }

        private void staffIDTxtArchive_KeyUp(object sender, KeyEventArgs e)
        {
            displayArchive();
        }

        private async void BtnArchive_Click(object sender, RoutedEventArgs e)
        {
            if (archiveStaffCurrentStatusTxt.Text == "Live")
            {
                Staff.archive(archiveStaffIDTxt.Text);
                Staff staff = await Staff.search(archiveStaffIDTxt.Text);
                archiveStaffCurrentStatusTxt.Text = staff.Status;
            }
            else if (archiveStaffCurrentStatusTxt.Text == "Invalid StudentID")
            {
                MessageBox.Show("The Staff ID you have entered is not valid");
            }
            else if (archiveStaffCurrentStatusTxt.Text == "")
            {
                MessageBox.Show("You must enter a complete Staff ID");
            }
            else
            {
                MessageBox.Show("The Staff member must currently have Live Status");
            }
        }

        private void CancelBtnArchive_Click(object sender, RoutedEventArgs e)
        {
            archiveStaffIDTxt.Text = "";
            archiveStaffFirstNameTxt.Text = "";
            archiveStaffMiddleNameTxt.Text = "";
            archiveStaffSurnameTxt.Text = "";
            archiveStaffDOBTxt.Text = "";
            archiveStaffCurrentStatusTxt.Text = "";
        }

        private async void staffSearch_KeyUp(object sender, KeyEventArgs e)
        {

            displayStaffResultsBox.Items.Clear();
            if (displayStaffSearch.Text.Length >= 3)
            {
                searchLoading.Visibility = Visibility.Visible;
                DataTable table = await Staff.search(displayStaffSearchType.Text, displayStaffSearch.Text);
                displayStaffResultsBox.Items.Add("StaffID\t\tFirstname\tLastname\tDOB\t\tStatus");
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    string data1 = table.Rows[i]["userID"].ToString() + "\t" + table.Rows[i]["firstname"].ToString() + "\t\t" + table.Rows[i]["lastname"].ToString() + "\t\t" + table.Rows[i]["DOB"].ToString() + "\t" + table.Rows[i]["status"].ToString();
                    displayStaffResultsBox.Items.Add(data1);
                }
                searchLoading.Visibility = Visibility.Hidden;
            }

        }
        public string DialogValue = "";
        private void resultsBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           if (displayStaffResultsBox.SelectedValue != null && displayStaffResultsBox.SelectedIndex != 0)
            {
                StaffDialogBox dialog = new StaffDialogBox(this);
                dialog.ShowDialog();
                if (DialogValue == "amend")
                {
                    AmendStaffIDTxt.Text = displayStaffResultsBox.SelectedValue.ToString().Substring(0, 9);
                    displayAmend();
                    subMenu.SelectedIndex = 1;
                }
                else if (DialogValue == "assign")
                {
                    //staffIDTxtEnrol.Text = displayStaffResultsBox.SelectedValue.ToString().Substring(0, 9);
                    subMenu.SelectedIndex = 3;
                }
                else if (DialogValue == "archive")
                {
                    archiveStaffIDTxt.Text = displayStaffResultsBox.SelectedValue.ToString().Substring(0, 9);
                    displayArchive();
                    subMenu.SelectedIndex = 2;
                }
                else
                {
                    displayStaffResultsBox.SelectedItem = null;
                }
                DialogValue = "";
            }
        }
        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }


        private async void createStaffSaveButton_Click(object sender, RoutedEventArgs e)
        {
            string email = await Database.newEmail(createStaffFirstNameTxt.Text, createStaffSurnameTxt.Text);
            string password = createStaffFirstNameTxt.Text.ToLower()[0] + createStaffSurnameTxt.Text.ToLower() + createStaffDOBTxt.Text.Substring(0, 4) + "=";
            createStaffSaveButton.Content = "Saving...";
            createStaffSaveButton.IsEnabled = false;
            Staff staff = new Staff();
            staff.ID = staffIDTxt.Text;
            staff.FirstName = createStaffFirstNameTxt.Text;
            staff.MiddleName = createStaffMiddleNameTxt.Text;
            staff.LastName = createStaffSurnameTxt.Text;
            staff.HomePhone = createStaffHomeNoTxt.Text;
            staff.MobilePhone = createStaffMobNoTxt.Text;
            staff.EmailUni = email + "@woodlandsuniversity.com";
            staff.EmailPersonal = "personal";
            staff.Line1 = createStaffLine1Txt.Text;
            staff.Line2 = createStaffLine2Txt.Text;
            staff.City = createStaffCityTxt.Text;
            staff.Postcode = createStaffPostcodeTxt.Text;
            staff.DOB = createStaffDOBTxt.Text;

            await staff.create();

            Mail.addMailBox(email,password);


            foreach (TextBox child in FindVisualChildren<TextBox>(createGrid))
            {
                child.Text = string.Empty;
            }

            staffIDTxt.Text = Staff.nextID();
            createStaffSaveButton.Content = "Save";
            createStaffSaveButton.IsEnabled = true;
        }

        private void createStaffCancelButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (TextBox child in FindVisualChildren<TextBox>(createGrid))
            {
                child.Text = string.Empty;
            }

            staffIDTxt.Text = Staff.nextID();
        }

        private async void name_KeyUp(object sender, KeyEventArgs e)
        {
            if (createStaffFirstNameTxt.Text.Length > 0 && createStaffSurnameTxt.Text.Length > 0)
            {
                string test = await Database.newEmail(createStaffFirstNameTxt.Text, createStaffSurnameTxt.Text) + "@woodlandsuniversity.com";
                if (test != "")
                {
                    createStaffWUCEmailTxt.Text = test;
                }
            }
            else
            {
                createStaffWUCEmailTxt.Text = "";
            }
        }

        private void amendStaffCancel_Click_1(object sender, RoutedEventArgs e)
        {
            AmendStaffIDTxt.Text = "";
            amendStaffFirstNameTxt.Text = "";
            amendStaffMiddleNameTxt.Text = "";
            amendStaffSurnameTxt.Text = "";
            amendStaffHomeNoTxt.Text = "";
            amendStaffMobNo.Text = "";
            amendStaffPersonalEmailTxt.Text = "";
            amendStaffLine1Txt.Text = "";
            amendStaffLine2Txt.Text = "";
            amendStaffCityTxt.Text = "";
            amendStaffPostcodeTxt.Text = "";
            amendStaffStatusCombo.SelectedIndex = 0;
            amendStaffFirstNameTxt.IsEnabled = false;
            amendStaffMiddleNameTxt.IsEnabled = false;
            amendStaffSurnameTxt.IsEnabled = false;
            amendStaffHomeNoTxt.IsEnabled = false;
            amendStaffMobNo.IsEnabled = false;
            amendStaffPersonalEmailTxt.IsEnabled = false;
            amendStaffLine1Txt.IsEnabled = false;
            amendStaffLine2Txt.IsEnabled = false;
            amendStaffCityTxt.IsEnabled = false;
            amendStaffPostcodeTxt.IsEnabled = false;
            amendStaffStatusCombo.IsEnabled = false;
        }

        private void amendStaffSave_Click_1(object sender, RoutedEventArgs e)
        {

        }
    }
}